package com.zgl.modules.wallet.controller;

import com.zgl.modules.card.entity.CardEntity;
import com.zgl.modules.card.service.impl.CardServiceImpl;
import com.zgl.modules.user.entity.User;
import com.zgl.modules.wallet.DTO.RechargeOrderDTO;
import com.zgl.modules.wallet.DTO.WalletDTO;
import com.zgl.modules.wallet.entity.WalletEntity;
import com.zgl.modules.wallet.service.impl.WalletServiceImpl;
import com.zgl.utils.Params;
import com.zgl.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@Api(tags = "钱包操作")
@RequestMapping("/wallet")
public class WalletController {
    @Resource
    private WalletServiceImpl walletService;
    @Resource
    private CardServiceImpl cardService;
    @PostMapping("/recharge")
    @ApiOperation(value = "从银行卡充值到钱包")
    @ResponseBody
    public R recharge(@RequestBody Params<RechargeOrderDTO> params) {
        final String headerToken = params.getParams().getToken();
        Claims claims = Jwts.parser().setSigningKey("zgl-key").parseClaimsJws(headerToken).getBody();
        Integer tokenUserId =  Integer.valueOf((String)claims.get("userid"));
        Object cardObj= cardService.getOne(params.getParams().getId()).getData();
        if(cardObj==null){
            return R.error("获取用户银行卡失败");
        }
        CardEntity cardEntity=(CardEntity)cardObj;
        double cardMoney = Double.valueOf(cardEntity.getCardBalance());
        double orderMoney = Double.valueOf(params.getParams().getMoney());
        if(cardMoney-orderMoney<0){
            return R.error("银行卡余额不足");
        }
        String newcardMoney=""+(cardMoney-orderMoney);
        cardEntity.setCardBalance(newcardMoney);
        cardService.modifyOne(cardEntity);
        Object walletObj=walletService.getCardByUserId(tokenUserId).getData();
        if(walletObj==null){
            return R.error("获取用户钱包失败");
        }
        WalletEntity walletEntity=(WalletEntity)walletObj;
        double balance = Double.valueOf(walletEntity.getBalance());
        double nWalletMoney=balance+orderMoney;
        String nWalletMonestr=""+nWalletMoney;
        walletEntity.setBalance( String.format("%.2f", nWalletMoney));
       return walletService.modifyOne(walletEntity);
    }

    @PostMapping("/cashout")
    @ApiOperation(value = "提现")
    @ResponseBody
    public R cashout(@RequestBody Params<RechargeOrderDTO> params) {
        final String headerToken = params.getParams().getToken();
        Claims claims = Jwts.parser().setSigningKey("zgl-key").parseClaimsJws(headerToken).getBody();
        Integer tokenUserId =  Integer.valueOf((String)claims.get("userid"));
        Object cardObj= cardService.getOne(params.getParams().getId()).getData();
        if(cardObj==null){
            return R.error("获取用户银行卡失败");
        }
        Object walletObj=walletService.getCardByUserId(tokenUserId).getData();
        if(walletObj==null){
            return R.error("获取用户钱包失败");
        }
        WalletEntity walletEntity=(WalletEntity)walletObj;
         double walletBalance= Double.valueOf( walletEntity.getBalance());
        CardEntity cardEntity=(CardEntity)cardObj;
        double cardMoney = Double.valueOf(cardEntity.getCardBalance());
        double orderMoney = Double.valueOf(params.getParams().getMoney());
        if(walletBalance-orderMoney<0){
            return R.error("零钱余额不足");
        }
        String newcardMoney=""+(cardMoney+orderMoney);
        cardEntity.setCardBalance(newcardMoney);
        cardService.modifyOne(cardEntity);
        double balance = Double.valueOf(walletEntity.getBalance());
        double nWalletMoney=balance-orderMoney;
        String nWalletMonestr=""+nWalletMoney;
        walletEntity.setBalance( String.format("%.2f", nWalletMoney));
        return walletService.modifyOne(walletEntity);
    }
    @ApiOperation(value = "查看当前用户钱包")
    @PostMapping("/requireWallet")
    @ResponseBody
    public R requireWallet(@RequestBody Params<RechargeOrderDTO> params) {
        final String headerToken = params.getParams().getToken();
        Claims claims = Jwts.parser().setSigningKey("zgl-key").parseClaimsJws(headerToken).getBody();
        String tokenUserId = (String) claims.get("userid");
        int itokenUserId=Integer.parseInt(tokenUserId);
        return walletService.getCardByUserId(itokenUserId);
    }

}
